package com.gitee.wsl.doc.prosemirror.ui

import com.gitee.wsl.doc.prosemirror.model.node.Node
import kotlin.reflect.KClass


class InlineNodeStringBuilders private constructor(
    private val annotatedStringBuilders: Map<KClass<out Node>, IInlineNodeStringBuilder<out Node>>
) {

    companion object {
        private val originalAnnotatedStringBuilders: Map<KClass<out Node>, IInlineNodeStringBuilder<out Node>> =
            mapOf(
                // Add default inline node string builders here if any
//                Text::class.java to TextNodeStringBuilder,
//                Paragraph::class.java to ParagraphNodeStringBuilder,
//                Image::class.java to ImageNodeStringBuilder,
//                Code::class.java to CodeNodeStringBuilder,
//                BulletList::class.java to BulletListNodeStringBuilder,
//                BulletListItem::class.java to BulletListItemNodeStringBuilder,
//                OrderedList::class.java to OrderedListNodeStringBuilder,
//                OrderedListItem::class.java to OrderedListItemNodeStringBuilder,
//                StrongEmphasis::class.java to StrongEmphasisNodeStringBuilder,
//                Emphasis::class.java to EmphasisNodeStringBuilder,
//                Subscript::class.java to SubscriptNodeStringBuilder,
//                Strikethrough::class.java to StrikethroughNodeStringBuilder,
//                SoftLineBreak::class.java to SoftLineBreakNodeStringBuilder,
//                HardLineBreak::class.java to HardLineBreakNodeStringBuilder,
//                Link::class.java to LinkNodeStringBuilder,
            )
    }

    class Builder {
        private val annotatedStringBuilders: MutableMap<KClass<out Node>, IInlineNodeStringBuilder<out Node>> =
            originalAnnotatedStringBuilders.toMutableMap()

        fun addAnnotatedStringBuilder(
            nodeClass: KClass<out Node>,
            builder: IInlineNodeStringBuilder<out Node>
        ): Builder {
            annotatedStringBuilders[nodeClass] = builder
            return this
        }

        fun build(): InlineNodeStringBuilders {
            val inlineNodeStringBuilders = InlineNodeStringBuilders(annotatedStringBuilders.toMap())
            return inlineNodeStringBuilders
        }
    }

    operator fun get(nodeClass: KClass<out Node>): IInlineNodeStringBuilder<Node>? {
        return annotatedStringBuilders[nodeClass] as? IInlineNodeStringBuilder<Node>
    }
}

val defaultInlineNodeStringBuilders by lazy {
    InlineNodeStringBuilders.Builder().build()
}
